﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace WizzardTheGame
{
    class Enemy:Character
    {
        private const Int32 MAX_PATROLTICS = 150;
        private EnemyState enemy_State;
        private Int32 start_patroltics;
        private Int32 patroltics;
        Random random = new Random();

        #region Properties

        public Random Random
        {
            get { return random; }
            set { random = value; }
        }

        public Int32 Start_patroltics
        {
            get { return start_patroltics; }
            set { start_patroltics = value; }
        }

        public Int32 Patroltics
        {
            get { return patroltics; }
            set { patroltics = value; }
        }

        public EnemyState Enemy_State
        {
            get { return enemy_State; }
            set { enemy_State = value; }
        }

        #endregion

        public enum EnemyState
        {
            patrol, attack
        }

        public Enemy():base()
        {
            Enemy_State = EnemyState.patrol;
            MoveVector = Vector3.Zero;
            CharModel.Rotation = GameWorld.STANDARD_ROTATION2;
            CharModel.Initial_y_rotation = GameWorld.STANDARD_ROTATION2.Y;
            RandomPatroltics();
        }

        public void Drop()
        {
            for(int i = 0; i < Inventory.Items.Count; i++) {
                if (Random.Next(11) > 8)
                {
                    if (Inventory.Items[i] != null)
                    {
                        Inventory.Items[i].Position = Position;
                        Inventory.Items[i].InInventory = false;
                        GameWorld.getInstance().Items.Add(Inventory.Items[i]);
                        GameWorld.getInstance().Drawables.Add(Inventory.Items[i]);
                        Inventory.Items.Remove(Inventory.Items[i]);
                    }
                }
            }
        }

        private void RandomPatroltics()
        {
            Start_patroltics = Random.Next(MAX_PATROLTICS) * 2;
            Patroltics = Start_patroltics;
        }

        public override void Update()
        {
            //Move
            if (Enemy_State == EnemyState.patrol)
            {
                Patroltics--;
                if (Patroltics > Start_patroltics/2 - 1)
                {
                    if (Patroltics == (Start_patroltics - 1))
                    {
                        CharModel.Rotation = GameWorld.STANDARD_ROTATION2;
                        //Position = Position + new Vector3(12f, 0f, 0f);
                    }
                    MoveVector = LeftVec;
                }
                if (Patroltics < Start_patroltics / 2)
                {
                    if (Patroltics == Start_patroltics/ 2 - 1)
                    {
                        CharModel.Rotation = GameWorld.STANDARD_ROTATION2 - new Vector3(0, MathHelper.Pi, 0);
                        //Position = Position + new Vector3(-12f, 0f, 0f);
                    }
                    MoveVector = RightVec;
                    if (Patroltics < 1) RandomPatroltics();
                }
            }

            base.Update();

            //Hvis den er død
            if (CurrentHP < 1)
            {
                IsAlive = false;
                Drop();
                GameWorld.getInstance().Enemies.Remove(this); //måske
                GameWorld.getInstance().Drawables.Remove(this); //måske
            }
        }

        protected override void Attack()
        {
            Enemy_State = EnemyState.attack;
        }
    }
}